package com.feiyu.hooktest;

import android.content.Context;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import static de.robv.android.xposed.XposedHelpers.getBooleanField;
import static de.robv.android.xposed.XposedHelpers.setBooleanField;

public class HookTest implements IXposedHookLoadPackage {

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if (lpparam.packageName.equals("com.feiyu.testdemo")){
            XposedBridge.log("开始hook： " + lpparam.packageName);

            /*
                Hook  hookTest方法，简单写法，hook的方法不带参数
                afterHookedMethod 是调用原方法后执行的，
                    param.setResult可以修改返回值
             */
            Class clazz=lpparam.classLoader.loadClass("com.feiyu.testdemo.MainActivity");
            findAndHookMethod(clazz, "hookTest", new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    XposedBridge.log("-----BEFORE hook-----");
                    XposedBridge.log("原返回值: " + param.getResult());

                }

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    param.setResult("你已被劫持");
                    XposedBridge.log("-----AFTER hook-----");
                    XposedBridge.log("现在的返回值："+"你已被劫持");
                }
            });
            /*
                findAndHookMethod 方法带参数的写法
                findAndHookMethod（类名，lpparam.classLoader，方法名，方法的参数1（String 就写String.class类推）,...方法的参数n,new XC_MethodHook() {}）
                    beforeHookedMethod 原方法调用前就执行的，可以用来修改传入方法中的参数

             */
            findAndHookMethod("com.feiyu.testdemo.MainActivity", lpparam.classLoader,"saveObject", Context.class, String.class,Object.class,new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    XposedBridge.log("-----BEFORE hook-----");
                    XposedBridge.log("参数1: " + param.args[0]);
                    XposedBridge.log("参数2: " + param.args[1]);
                    XposedBridge.log("参数3: " + param.args[2]);
                    XposedBridge.log("参数3 类的isVip的值: " + getBooleanField(param.args[2],"isVip"));//此处
                    setBooleanField(param.args[2],"isVip",true);
                }

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    XposedBridge.log("-----AFTER hook-----");
                }
            });
        }else {
            return;
        }
    }
}
